package org.opentcs.kcvehicle.communication.kc.udp.Service;

import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import org.opentcs.kcvehicle.communication.kc.common.byteutils.ByteUtils;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.AgvEvent;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.AgvEventConstant;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.write.WriteParam;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.write.WriteStrValue;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.write.WriteValueMember;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.rsp.RcvEventPackage;
import org.opentcs.kcvehicle.communication.kc.udp.io.UDPClient;

public class SwitchManualMode
    extends
      BaseCommand {

  /**
   * decs: write操作
   * 指令：0x03
   * author: caixiang
   * date: 2025/1/17 16:25
   */
  public static AgvEvent writeValue() {
    AgvEvent agvEvent = new AgvEvent(AgvEventConstant.CommandCode_WRITE);

    List<WriteValueMember> valueMemberList = new ArrayList<>();
    WriteValueMember valueMember1 = new WriteValueMember(
        Short.valueOf("0"), Short.valueOf("4"), ByteUtils.uintToBytes(3, ByteOrder.LITTLE_ENDIAN)
    );
    valueMemberList.add(valueMember1);

    List<WriteStrValue> strValueList = new ArrayList<>();
    WriteStrValue strValue = new WriteStrValue("TestRW", 1, valueMemberList);
    strValueList.add(strValue);

    WriteParam param = new WriteParam(1, strValueList);
    agvEvent.setBody(param.toBytes());

    return agvEvent;
  }

  public static void command() {
    //0x03(切换手自动)
    AgvEvent agvEvent = writeValue();
    printInfo(agvEvent);
    RcvEventPackage rcv = UDPClient.localAGV.send(agvEvent);
    if (rcv.isOk()) {
      System.out.println();
      System.out.println("received " + "isok:" + rcv.isOk() + " dataBytes:");
      printInfo(rcv);
      if (rcv.isOk()) {
        //get and parse value
        System.out.println("write ok");
      }
      else {
        System.out.println("write failed");
      }
    }
    else {
      System.out.println();
      System.out.println("received transationId : " + "isok:" + rcv.isOk());
    }
  }
}
